<?php

namespace App\Admin\Controllers;

use App\model\Biaobiao;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class BiaobiaoController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('消费报表')
            ->description('description')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('Detail')
            ->description('description')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('Edit')
            ->description('description')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('Create')
            ->description('description')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Biaobiao);
        $grid->model()->orderBy('id','desc');
        $grid->id('Id');
        $grid->time('日期');
        $grid->project_name('项目名称');
        $grid->source_name('渠道');
        $grid->ruke_num('入客数');
        $grid->xiaofei('消费');
        $grid->ruke('入客均价')->display(function($value){
            if ($this->ruke_num==0){
                return '无法计算';
            }
            return round($this->xiaofei/$this->ruke_num,2);
        });
        $grid->xianchang('已看现场');
        $grid->daochang('到场率')->display(function($value){
            if ($this->ruke_num==0){
                return '无法计算';
            }
           return round( ($this->xianchang/$this->ruke_num)*100,2).'%';
        });
        $grid->daochangPrice('到场均价')->display(function($value){
            if ($this->daochang==0){
                return '无法计算';
            }
            return round($this->xiaofei/$this->daochang,2);
        });

        $grid->chengjiao('成交数');
        $grid->chengjiaolv('成交率')->display(function($value){
            if ($this->chengjiao==0){
                return '无法计算';
            }
            return round( ($this->chengjiao/$this->ruke_num)*100,2).'%';
        });
        $grid->daochangPrice('成交均价')->display(function($value){
            if ($this->chengjiao==0){
                return '无法计算';
            }
            return round($this->xiaofei/$this->chengjiao,2);
        });


        $grid->disableCreateButton();
        $grid->disableExport();
        $grid->disableActions();
        $grid->filter(function(Grid\Filter $filter){
            $filter->disableIdFilter();
            $filter->in('project_name','项目名称')->select(Biaobiao::select('project_name')->groupBy('project_name')->pluck('project_name','project_name')->toArray());
;
            $filter->in('source_name','渠道名称')->select(Biaobiao::select('source_name')->groupBy('source_name')->pluck('source_name','source_name')->toArray());
            ;
            $filter->between('time','日期')->date();
        });
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Biaobiao::findOrFail($id));

        $show->id('Id');
        $show->source_name('Source name');
        $show->user_name('User name');
        $show->ruke_num('Ruke num');
        $show->xiaofei('Xiaofei');
        $show->xianchang('Xianchang');
        $show->chengjiao('Chengjiao');
        $show->created_at('Created at');
        $show->updated_at('Updated at');
        $show->time('Time');
        $show->project_name('Project name');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Biaobiao);

        $form->text('source_name', 'Source name');
        $form->text('user_name', 'User name');
        $form->number('ruke_num', 'Ruke num');
        $form->decimal('xiaofei', 'Xiaofei');
        $form->number('xianchang', 'Xianchang');
        $form->number('chengjiao', 'Chengjiao');
        $form->datetime('time', 'Time')->default(date('Y-m-d H:i:s'));
        $form->text('project_name', 'Project name');

        return $form;
    }
}
